<template>
  <div class="main">
    <div class="main-div">
      <keep-alive>
        <router-view v-if="!$route.meta.keepAlive"></router-view>
        <!--这里是不会被缓存的组件-->
      </keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </div>
    <van-tabbar v-model="active" @change="changeTabbar(active)">
      <van-tabbar-item icon="shop">首页</van-tabbar-item>
      <van-tabbar-item icon="records">列表页</van-tabbar-item>
      <van-tabbar-item icon="cart">购物车页</van-tabbar-item>
      <van-tabbar-item icon="contact" info="20">会员中心</van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
  export default {
    name: 'Main',
    data() {
      return {
        active: 0,
      }
    },
    created(){
      this.changeTabBarActive()
    },
    updated() {
      this.changeTabBarActive()
    },
    methods: {
      // 底部tab点击时
      changeTabbar(active) {
        switch(active){
          case 0:
            this.$router.push({name:'home'})
            break;
          case 1:
            this.$router.push({name:'CategoryList'})
            break;
          case 2:
            this.$router.push({name:'Cart'})
            break;
          case 3:
            this.$router.push({name:'Vip'})
            break;
          default :
            break;
        }
      },
      // 进入页面时，默认选中tab
      changeTabBarActive(){
        this.nowPath=this.$route.path;
        switch (this.nowPath) {
          case '/':
            this.active=0;
            break;
          case '/gategoryList':
            this.active=1;
            break;
          case '/cart':
            this.active=2;
            break;
          case '/vip':
            this.active=3;
            break;
          default:
            break;
        }
      },
    }
  }
</script>

<style lang="stylus" scoped>
@import '~@/assets/css/common.styl';
.main {
  /deep/ .van-tabbar {
    background-color: $colorB;
  }
}
.main-div {
  padding-bottom: 50px;
}
</style>